<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议 https://mit-license.org
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\api\controller\system;

use app\admin\service\TriggerService;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use app\admin\service\NodeService;
use think\App;
use think\Model;
use app\admin\model\SystemRoleMenu;
use app\admin\model\SystemMenu;

/**
 * @ControllerAnnotation(title="角色管理")
 * Class Node
 *
 * @package app\admin\controller\system
 */
class Menu extends AdminController
{

    /**
     * 字段排序
     *
     * @var array
     */
    protected $sort = [
        'sort_number' => 'desc'
    ];
    
    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\SystemMenu();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $request = $this->request;
        $tenant_id = $request->_TENANTID;
        
        $page = input("page");
        $limit = input("limit");
        
        $where[] = [
            "tenant_id",
            "=",
            $tenant_id
        ];
        
        if (! empty(input('title'))) {
            $where[] = [
                "title",
                "LIKE",
                "%" . input('title') . "%"
            ];
        }
        
        if (! empty(input('path'))) {
            $where[] = [
                "path",
                "LIKE",
                "%" . input('path') . "%"
            ];
        }
        
        if (! empty(input('authority'))) {
            $where[] = [
                "authority",
                "LIKE",
                "%" . input('authority') . "%"
            ];
        }
        
        $count = $this->model->where($where)->count();
        
        if (! empty($page) && ! empty($limit)) {
            $list = $this->model->where($where)
                ->field("*,menu_id menuId,parent_id parentId,menu_type menuType,sort_number sortNumber,create_time createTime")
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
        } else {
            $list = $this->model->where($where)
                ->field("*,menu_id menuId,parent_id parentId,menu_type menuType,sort_number sortNumber,create_time createTime")
                ->order($this->sort)
                ->select();
        }
        
        return msg($list, 0, "success");
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        $request = $this->request;
        $tenant_id = $request->_TENANTID;
        $post = $this->request->post();
        $rule = [];
        $this->validate($post, $rule);
        try {
            $post['tenant_id'] = $tenant_id;
            $post['parent_id'] = $post['parentId'];
            $post['menu_type'] = $post['menuType'];
            $post['sort_number'] = $post['sortNumber'];
            $post['create_time'] = date('Y-m-d H:i:s', time());
            $post['update_time'] = date('Y-m-d H:i:s', time());
            $save = $this->model->save($post);
        } catch (\Exception $e) {
            return msg([], 1, "保存异常:" . $e->getMessage());
        }
        return $save ? msg([], 0, "保存成功") : msg([], 1, "保存失败");
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $request = $this->request;
        $tenant_id = $request->_TENANTID;
        $post = $this->request->post();
        $id = $post['menuId'];
        $row = $this->model->where("menu_id", $id)->find();
        if (empty($row)) {
            return msg([], 1, "数据不存在");
        }
        $post = $this->request->post();
        $rule = [];
        $this->validate($post, $rule);
        try {
            $post['tenant_id'] = $tenant_id;
            $post['parent_id'] = $post['parentId'];
            $post['menu_type'] = $post['menuType'];
            $post['sort_number'] = $post['sortNumber'];
            $post['update_time'] = date('Y-m-d H:i:s', time());
            $save = $row->save($post);
        } catch (\Exception $e) {
            return msg([], 1, "编辑异常:" . $e->getMessage());
        }
        return $save ? msg([], 0, "编辑成功") : msg([], 1, "编辑失败");
    }

    /**
     * @NodeAnotation(title="删除")
     */
    public function delete()
    {
        $id = input('id');
        $row = $this->model->whereIn('menu_id', $id)->select();
        if (empty($row)) {
            return msg([], 1, "数据不存在");
        }
        try {
            $save = $this->model->whereIn('menu_id', $id)->delete();
        } catch (\Exception $e) {
            return msg([], 1, "删除失败:" . $e->getMessage());
        }
        return $save ? msg([], 0, "删除成功") : msg([], 1, "删除失败");
    }
}